﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;

/// <summary>
/// Represents user control to handle operations on daily scrum object. Display, edit, create, delete daily scrum
/// </summary>
public partial class DailyScrumDetails : System.Web.UI.UserControl
{
    /// <summary>
    /// User that is daily scrum entry tied to
    /// </summary>
    public User forUser { get; set; }

    /// <summary>
    /// Available tasks for specific user that she/he is asociated with
    /// </summary>
    public List<Task> tasks { get; set; }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        AllTasks.DataSource = tasks;
        AllTasks.DataBind();
    }

    /// <summary>
    /// Sets all data and visibility in DailyScrumDetails for selected dailyScrum
    /// </summary>
    /// <param name="rel">selected bli</param>
    public void ShowDailyScrumDetails(DailyScrum ds)
    {
        this.Visible = true;

        bool exists = ds != null;

        StartDate.SeledtedDate = exists ? ds.Date.Date : DateTime.Now ;
        Done.Text = exists ? ds.Did : "";
        ToDo.Text = exists ? ds.ToDo : "";
        Impediments.Text = exists ? ds.Problems : "";
        Comments.Text = exists ? ds.Comments : "";
        BurnDown.Value = exists ? ds.StoryPoints : 0;
        if (tasks == null)
        {
            AllTasks.Items.Clear();
            AllTasks.Items.Insert(0, new ListItem((string)GetLocalResourceObject("NoTasks"), String.Empty));
            AllTasks.SelectedIndex = 0;
        }
        else 
        {
            AllTasks.DataSource = tasks;
            AllTasks.DataBind();
            AllTasks.SelectedIndex = -1;
        }
        if (ds.TaskID != null && ds.TaskID != -1 && ds.TaskID != 0)
        {
            AllTasks.SelectedValue = ds.TaskID.ToString();
        }
    }
    /// <summary>
    /// Creates new DailyScrum
    /// </summary>
    /// <returns>Created DailyScrum</returns>
    public DailyScrum CreateNewDailyScrum()
    {
        if(forUser == null)
        { 
            throw new Exception("User Not set for daily scrum");
        }
        DailyScrum newDS = new DailyScrum(0, StartDate.SeledtedDate.Value, forUser);
        newDS.StoryPoints = BurnDown.Value;
        newDS.Comments = Comments.Text;
        newDS.Did = Done.Text;
        newDS.ToDo = ToDo.Text;
        newDS.Problems = Impediments.Text;
        newDS.Task = DAOTask.GetTaskByID(System.Convert.ToInt32(AllTasks.SelectedValue));
        DailyScrum ds = DaoDailyScrum.CreateNewDailyScrumDetail(newDS);
        return ds;
    }

    /// <summary>
    /// Edits daily scrum according to data in daily scrum detail panel
    /// </summary>
    /// <param name="dailyOldID">ID of daily scrum detail to update</param>
    public DailyScrum EditDailyScrum(int dailyOldID)
    {
        if (forUser == null)
        {
            throw new Exception("User Not set for daily scrum");
        }
        DailyScrum newDS = new DailyScrum(dailyOldID, StartDate.SeledtedDate.Value, forUser);
        newDS.StoryPoints = BurnDown.Value;
        newDS.Comments = Comments.Text;
        newDS.Did = Done.Text;
        newDS.ToDo = ToDo.Text;
        newDS.Problems = Impediments.Text;
        newDS.Task = DAOTask.GetTaskByID(System.Convert.ToInt32(AllTasks.SelectedValue));
        DaoDailyScrum.UpdateDailyScrum(newDS);
        this.Visible = false;
        //showDailyScrumDetails(newDS);
        return newDS;
    }

    /// <summary>
    /// Deletes DailyScrum
    /// </summary>
    /// <param name="ds">DailyScrum to delete</param>
    public void DeleteDailyScrum(DailyScrum ds)
    {
        DaoDailyScrum.DeleteDailyScrum(ds.ID);
        this.Visible = false;
    }

}
